import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class MySplitter {

    /**
     * The split body method returns something that is iteratable such as a java.util.List.
     *
     * @param body the payload of the incoming message
     * @return a list containing each part splitted
     * @throws ParserConfigurationException 
     * @throws IOException 
     * @throws SAXException 
     * @throws XPathExpressionException 
     */
    public String splitBody(String body) {
        // since this is based on an unit test you can of cause
        // use different logic for splitting as Camel have out
        // of the box support for splitting a String based on comma
        // but this is for show and tell, since this is java code
        // you have the full power how you like to split your messages
    	
//        List<String> answer = new ArrayList<String>();
//        String[] parts = body.split(",");
        
        
    	
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        Object result = null;
        
        try {
	        DocumentBuilder builder = domFactory.newDocumentBuilder();
	        
	        InputSource is = new InputSource( new StringReader( body ) );
	        
	        Document doc = builder.parse(is);
	
	        XPathFactory factory = XPathFactory.newInstance();
	        XPath xpath = factory.newXPath();
	        XPathExpression expr 
	         = xpath.compile("/rss/channel/item");
	
	        result = expr.evaluate(doc, XPathConstants.NODESET);
	        
	        NodeList nodes = (NodeList) result;
	        NodeList childs;
	        for (int i = 0; i < nodes.getLength(); i++) {
	            childs = nodes.item(i).getChildNodes(); 
//	            for (int j = 0; j < childs.getLength(); j++) {
//					System.out.print(j +": "+ childs.item(j).getTextContent());
//					
//				}
	            System.out.println("<earthquake>");
	            System.out.println(" <region>" + childs.item(1).getTextContent().substring(7) + "</region>");
	            System.out.println(" <date>" + childs.item(5).getTextContent().substring(0, 19) + "</date");
	            System.out.println(" <magnitude>" + childs.item(1).getTextContent().substring(2,5) + "</magnitude>");
	            String[] oha = childs.item(5).getTextContent().substring(22).replaceAll("   ", " ").split(" ");
	            if(oha[1].equals("N")){
	            	System.out.println(" <latitude>" + oha[0] + "</latitude>");
	            }else{
	            	System.out.println(" <latitude>-" + oha[0] + "</latitude>");
	            }
	            if(oha[3].equals("E")){	
	            	System.out.println(" <longitude>" + oha[2] + "</longitude>");
	            }else{
	            	System.out.println(" <longitude>-" + oha[2] + "</longitude>");
	            }
	            System.out.println(" <depth>" + oha[4] + "</depth>");
	            System.out.println("</earthquake>");
	            System.out.println("----");
	            
	        }
	        
        } catch (ParserConfigurationException pce) {
        	// TODO Auto-generated catch block
			pce.printStackTrace();
        } catch (SAXException se) {
			// TODO Auto-generated catch block
			se.printStackTrace();
		} catch (IOException ioe) {
			// TODO Auto-generated catch block
			ioe.printStackTrace();
		} catch (XPathExpressionException xee) {
			// TODO Auto-generated catch block
			xee.printStackTrace();
		}
		
		
		
        
//        for (String part : parts) {
//            answer.add(part);
//        }
//        return answer;
        
        return null;
    }
}
